package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.BeanUtils;

import java.util.Date;

/**
 * @Since 2022/12/28 22:30
 * @Author LA
 * @Description: 处理商场管理：通用问题相关功能
 */
@RestController
@RequestMapping("admin/issue")
public class IssueController {

    @Autowired
    IssueService issueService;

    @RequestMapping("list")
    // http://182.92.235.201:8083/admin/issue/list?page=1&limit=20&question=测试&sort=add_time&order=desc
    public BaseRespVo list(BasePageInfo info, String question) {
        // 响应中data 为CommonData类型
        // CommonData中的list 为MarketIssue类型
        // 传参，调用 Service层的list方法
        CommonData<MarketIssue> data = issueService.list(info, question);
        // 传入data数据，调用BaseRespVo.ok进行响应
        return BaseRespVo.ok(data);
    }


    // {
    //   "question": "LAadd",
    //   "answer": "add  answer"
    // }
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketIssue marketIssue) {
        marketIssue.setAddTime(new Date());
        marketIssue.setUpdateTime(new Date());
        // 调用新增issue方法
        issueService.create(marketIssue);
        // 响应结果
        // {
        // 	"errno": 0,
        // 	"data": {
        // 		"id": 33,
        // 		"question": "LAadd",
        // 		"answer": "add  answer",
        // 		"addTime": "2022-12-29 20:03:46",
        // 		"updateTime": "2022-12-29 20:03:46"
        // 	},
        // 	"errmsg": "成功"
        // }
        // new返回值的对象,并填值
        MarketIssue returnData = new MarketIssue();
        // 调用工具进行数据的copy
        BeanUtils.copyProperties(marketIssue, returnData);
        // returnData.setId(marketIssue.getId());
        // returnData.setQuestion(marketIssue.getQuestion());
        // returnData.setAnswer(marketIssue.getAnswer());
        // returnData.setAddTime(marketIssue.getAddTime());
        // returnData.setUpdateTime(marketIssue.getUpdateTime());
        // 响应结果
        BaseRespVo baseRespVo = BaseRespVo.ok(returnData);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;

    }


    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketIssue marketIssue) {
        // 调用service 进行删除操作
        issueService.delete(marketIssue);
        // 响应
        // {"errno":0,"errmsg":"成功"}
        return BaseRespVo.ok("成功");
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketIssue marketIssue) {
        //  调用service更新, 入参,出参
        MarketIssue issue = issueService.update(marketIssue);
        return BaseRespVo.ok(issue);
    }
}
